package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
)

func main() {
	postUserInfo([]string{"afans"}, []string{"123456"}, []string{"football", "basketball"})
	getUserInfo()
}

func getUserInfo() error {
	resp, err := http.Get("http://localhost/path?id=1234&name=Afans&value=&hobbies=football&hobbies=basketball") // hobbies出现两次，为数组
	if err != nil {
		fmt.Println("get err:", err)
		return err
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("readall, err:", err)
		return err
	}

	fmt.Println(string(body))

	return nil
}

func postUserInfo(username, password, hobbies []string) error {
	// resp, err := http.Post("http://localhost/user?username=liuxd&password=1234", "application/x-www.form-urlencoded", strings.NewReader("username=afans"))
	resp, err := http.PostForm("http://localhost/user?username=liuxd&password=1234",
		url.Values{
			"username": username,
			"password": password,
			"hobbies":  hobbies,
		})
	if err != nil {
		fmt.Println("post err:", err)
		return err
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("readall, err:", err)
		return err
	}

	fmt.Println(string(body))

	return nil
}
